package com.ruoyi.generator.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.generator.domain.Database;
import com.ruoyi.generator.mapper.DatabaseMapper;
import com.ruoyi.generator.sdk.MySqlConnectionClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.generator.mapper.GeneratedRecordsMapper;
import com.ruoyi.generator.domain.GeneratedRecords;
import com.ruoyi.generator.service.IGeneratedRecordsService;

/**
 * 代码生产记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-26
 */
@Service
public class GeneratedRecordsServiceImpl implements IGeneratedRecordsService 
{
    @Autowired
    private GeneratedRecordsMapper generatedRecordsMapper;

    @Autowired
    private DatabaseMapper databaseMapper;

    /**
     * 查询代码生产记录
     * 
     * @param id 代码生产记录主键
     * @return 代码生产记录
     */
    @Override
    public GeneratedRecords selectGeneratedRecordsById(Long id)
    {
        return generatedRecordsMapper.selectGeneratedRecordsById(id);
    }

    /**
     * 查询代码生产记录列表
     * 
     * @param generatedRecords 代码生产记录
     * @return 代码生产记录
     */
    @Override
    public List<GeneratedRecords> selectGeneratedRecordsList(GeneratedRecords generatedRecords)
    {
        return generatedRecordsMapper.selectGeneratedRecordsList(generatedRecords);
    }

    /**
     * 新增代码生产记录
     * 
     * @param generatedRecords 代码生产记录
     * @return 结果
     */
    @Override
    public int insertGeneratedRecords(GeneratedRecords generatedRecords)
    {
        generatedRecords.setCreateTime(DateUtils.getNowDate());
        return generatedRecordsMapper.insertGeneratedRecords(generatedRecords);
    }

    /**
     * 修改代码生产记录
     * 
     * @param generatedRecords 代码生产记录
     * @return 结果
     */
    @Override
    public int updateGeneratedRecords(GeneratedRecords generatedRecords)
    {
        generatedRecords.setUpdateTime(DateUtils.getNowDate());
        return generatedRecordsMapper.updateGeneratedRecords(generatedRecords);
    }

    /**
     * 批量删除代码生产记录
     * 
     * @param ids 需要删除的代码生产记录主键
     * @return 结果
     */
    @Override
    public int deleteGeneratedRecordsByIds(Long[] ids)
    {
        return generatedRecordsMapper.deleteGeneratedRecordsByIds(ids);
    }

    /**
     * 删除代码生产记录信息
     * 
     * @param id 代码生产记录主键
     * @return 结果
     */
    @Override
    public int deleteGeneratedRecordsById(Long id)
    {
        return generatedRecordsMapper.deleteGeneratedRecordsById(id);
    }

    /**
     * 查询数据库中可生成的表
     * @param generatedRecords
     * @return
     */
    @Override
    public List<Map<String, Object>> queryList(GeneratedRecords generatedRecords) {
        if (!generatedRecords.getGenStatus().equals("4")){
            throw new ServiceException("审核未通过！生成代码失败");
        }
        Database database = databaseMapper.selectDatabaseById(generatedRecords.getMainBaseId());

        MySqlConnectionClient mySqlConnectionClient = new MySqlConnectionClient.Builder()
                .mySqlIp(database.getBaseIp())//Ip
                .mySqlPort(database.getBasePort())//端口
                .databaseName(database.getBaseName())//数据库名
                .databasePassword(database.getBasePassword())//数据库密码
                .databaseUsername(database.getBaseUsername())//数据库用户名
                .build();
        //TODO 执行

        return List.of();
    }


}
